//
//  HXLDomTableViewCell.m
//  On The Load
//
//  Created by 晓琳 on 15/11/9.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLDomTableViewCell.h"
#import "HXLDomCollectionViewCell.h"
#import "DataHandle.h"
#import "HXLCityModel.h"
#import "HXLLeavesModel.h"


@interface HXLDomTableViewCell ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *placeArray;

@end

@implementation HXLDomTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}


- (void) createSubviews{

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(60*SCALEW, 30*SCALEH);
    flowLayout.minimumInteritemSpacing = 20*SCALEW;
    flowLayout.minimumLineSpacing = 10*SCALEH;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 30, 20);
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.height) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    [self addSubview:collectionView];
    
    [collectionView registerClass:[HXLDomCollectionViewCell class] forCellWithReuseIdentifier:@"dom1Reuse"];
    
    self.collectionView = collectionView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    //如果没有值则返回0,否则Crash
    if (self.cityModel == nil && self.placeArray.count == 0) {
        return 0;
    }
    
    if (160*SCALEH == self.height) {
        return 12;
    }else {
        return 22;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXLDomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dom1Reuse" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    
    
    if (_cityModel != nil) {
        //小model
        HXLLeavesModel *model = [self.cityModel.leavesArray objectAtIndex:indexPath.row];
        
        cell.contentLabel.text = model.cityName;
    }else{
        //大model
        HXLCityModel *model = [self.cityArray objectAtIndex:indexPath.row];
        cell.contentLabel.text = model.name;
    }
    
    
    return cell;
}


- (void)setHeight:(CGFloat)height{
    
    _height = height;
    
    self.collectionView.frame = CGRectMake(0, 0, WIDTH, self.height);
    
    [self.collectionView reloadData];
}

- (void)setCityModel:(HXLCityModel *)cityModel{

    if (cityModel != nil) {
        _cityModel = cityModel;
    }else{
        self.placeArray = [[DataHandle shareDataHandle].finDic objectForKey:@"areas"];
    }
    
    [self.collectionView reloadData];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_cityModel != nil) {
        //小model
        HXLLeavesModel *model = [self.cityModel.leavesArray objectAtIndex:indexPath.row];
        NSLog(@"cell = %@",self.myDelegate);

        [self.myDelegate getModelValueForPlanViewController:model];
    }else{
        //大model
        HXLCityModel *model = [self.cityArray objectAtIndex:indexPath.row];

        [self.myDelegate getTableViewForModel:model];
               
        

    }
}

@end
